package com.application.outdoor.adapter;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.application.Activity.R;
import com.application.outdoor.adapter.TinNhanAdapter.ViewHolder;
import com.application.outdoor.pojo.FileSharePOJO;
import com.application.outdoor.pojo.NguoiDungPOJO;
import com.application.outdoor.pojo.TinNhanPOJO;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class FileShareAdapter extends BaseAdapter {
	private static class ViewHolder {
		ImageView imgHinhAnh;
		TextView txtTenFile, txtNguoiUp, txtThoiGian;
//		Button btnDownload;
	}
	ArrayList<FileSharePOJO> data;
	Context context;
	private LayoutInflater inflater;
	public FileShareAdapter(Context context,ArrayList<FileSharePOJO> data) {
		this.context = context;
		this.data = data;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int index) {
		return data.get(index);
	}

	@Override
	public long getItemId(int position) {
		return data.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = inflater.inflate(R.layout.od_row_fileshare, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.imgHinhAnh=(ImageView)view.findViewById(R.id.imgHinhAnh);
			viewHolder.txtTenFile = (TextView) view.findViewById(R.id.txtTenFile);
			viewHolder.txtNguoiUp = (TextView) view.findViewById(R.id.txtNguoiUp);
			viewHolder.txtThoiGian = (TextView) view.findViewById(R.id.txtThoiGian);
//			viewHolder.btnDownload = (Button) view.findViewById(R.id.btnDownload);
			view.setTag(viewHolder);
		}
		
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.txtTenFile.setText(data.get(position).getTenFile());
		holder.txtNguoiUp.setText("Chia sẻ bởi: "+data.get(position).getNguoiUp()+"  vào lúc: "+new SimpleDateFormat("HH:mm:ss dd-MM-yyyy").format(new java.util.Date(data.get(position).getThoiGian())));
		holder.txtThoiGian.setText("Mô tả: "+ data.get(position).getGhiChu());
//		holder.txtThoiGian.setText("tao lao");
		String fName=data.get(position).getTenFile();
		int dotIndex=fName.lastIndexOf('.');
		String extendName="file";
		if(dotIndex>0){
			extendName=fName.substring(dotIndex,fName.length()).toLowerCase();
		}
		if(extendName.equals(".pdf")){
			holder.imgHinhAnh.setImageResource((R.drawable.ic_file_pdf));
		}else if(extendName.equals(".txt")){
			holder.imgHinhAnh.setImageResource((R.drawable.ic_file_txt));
		}else if(extendName.equals(".doc")||extendName.equals(".docx")){
			holder.imgHinhAnh.setImageResource((R.drawable.ic_file_doc));
		}else if(extendName.equals(".jpg")||extendName.equals(".jpeg")||extendName.equals(".bmp")||extendName.equals(".png")){
			holder.imgHinhAnh.setImageResource((R.drawable.ic_file_image));
		}
		else {
			holder.imgHinhAnh.setImageResource((R.drawable.ic_file_unknown));
		}
		
		return view;
	}

}
